//
// Created by Luhang on 2023/4/14/014.
//
#include "Data.h"

Data::Data(int year, int month, int day){
    //检查一下日期是否合法
    if (month > 0 && month < 13 && (day > 0 && day <= GetMonthDay(year, month))){
        _year = year;
        _month = month;
        _day = day;
    } else
        cout << "Wrong Data" << endl;
}

int Data::GetMonthDay(int year, int month) const{
    assert(month > 0 && month < 13);
    int monthArray[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    if(month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0))
        return 29;
    else
        return monthArray[month];
}

Data& Data::operator+=(int day) {
    if (day < 0){
        *this -= -day;
        return *this;
    }
    _day += day;
    while (_day > GetMonthDay(_year, _month)){
        _day -= GetMonthDay(_year, _month);
        ++_month;
        if (_month == 13){
            ++_year;
            _month = 1;
        }
    }
    return *this;
}

Data Data::operator+(int day) const {
    Data tmp = *this;
    tmp += day;
    return tmp;
}

Data &Data::operator-=(int day) {
    if (day < 0){
        *this += -day;
        return *this;
    }
    _day -= day;
    while (_day <= 0){
        --_month;
        if (_month == 0){
            --_year;
            _month = 12;
        }
        _day += GetMonthDay(_year, _month);
    }
    return *this;
}

Data Data::operator-(int day) {
    Data tmp = *this;
    tmp -= day;
    return tmp;
}

int Data::operator-(const Data &d) const {
    Data max = *this;
    Data min = d;
    int flag = 1;
    if(*this < d){
        max = d;
        min = *this;
        flag = -1;
    }
    int count = 0;
    while (min != max){
        ++min;
        ++count;
    }
    return count * flag;
}

Data &Data::operator++() {
    *this += 1;
    return *this;
}

Data Data::operator++(int) {
    Data tmp(*this);
    *this += 1;
    return tmp;
}

Data &Data::operator--() {
    *this -= 1;
    return *this;
}

Data Data::operator--(int) {
    Data tmp(*this);
    *this -= 1;
    return tmp;
}
